package com.lzx.controller;

import cn.hutool.core.bean.BeanUtil;
import com.github.pagehelper.PageInfo;
import com.lzx.api.CommonResult;
import com.lzx.entity.Order;
import com.lzx.entity.Orderitem;
import com.lzx.entity.Product;
import com.lzx.service.*;
import com.lzx.util.PageUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

/**
 * (Order)表控制层
 *
 * @author makejava
 * @since 2021-02-27 16:16:26
 */
@RestController
@RequestMapping("order")
public class OrderController {
    /**
     * 服务对象
     */
    @Autowired
    private OrderService orderService;
    @Autowired
    private OrderitemService orderitemService;
    @Autowired
    private ProductService productService;

    /**
     * 通过主键查询单条数据
     *
     * @param id 主键
     * @return 单条数据
     */
    @GetMapping("selectOne")
    public Order selectOne(Integer id) {
        return this.orderService.queryById(id);
    }

    /**
     * 创建新订单
     *
     * @param map
     * @return
     */
    @RequestMapping("/addOrder")
    @Transactional(rollbackFor = {RuntimeException.class, Error.class})
    public CommonResult addOrder(@RequestBody HashMap<String, Object> map) {
        Integer addressId = (Integer) map.get("addressId");
        ArrayList<Integer> cartIds = (ArrayList<Integer>) map.get("cartIds");
        Integer memberId = (Integer) map.get("memberId");
        List<Orderitem> orderitemList = orderService.createOrder(memberId, addressId, cartIds);
        orderitemList.forEach(x -> {
            Product product = productService.getById(x.getProductid());
            Integer salescount = product.getSalescount();
            if (!BeanUtil.isNotEmpty(salescount)) {
                salescount = 0;
            }
            product.setSalescount(salescount + x.getCount());
            productService.update(product);
        });
        orderitemService.saveBatch(orderitemList);
        return CommonResult.success("结算成功");
    }

    @GetMapping("/list")
    public CommonResult listOrder(@RequestParam(required = false) String query, @RequestParam(defaultValue = "1") Integer pagenum, @RequestParam(defaultValue = "5") Integer pagesize) {

        List<Order> orderList = orderService.list();

        PageInfo<Order> pageInfo = PageUtil.startPage(orderList, pagenum, pagesize);
        return CommonResult.success(pageInfo);
    }
    @PutMapping("/updateState")
    public CommonResult changeState(Integer orderId){
        Order thisOrder = orderService.getById(orderId);
        if(BeanUtil.isNotEmpty(thisOrder)){
            thisOrder.setState("已收货");
            orderService.update(thisOrder);
            return CommonResult.success("已确认收货");
        }
        return CommonResult.failed("订单不存在");
    }
}
